#include "napi/native_api.h"
#include <js_native_api.h>
#include <js_native_api_types.h>
#include <hilog/log.h>
#define LOG_DOMAIN 0
#define LOG_TAG "demoTest"

//https://blog.csdn.net/qq_38128179/article/details/106012003
//https://nodejs.org/api/n-api.html#functions-to-convert-from-node-api-to-c-types
//https://blog.51cto.com/u_15127619/4154233


static napi_value Add(napi_env env, napi_callback_info info)
{
    size_t requireArgc = 2;
    size_t argc = 2;
    napi_value args[2] = {nullptr};

    napi_get_cb_info(env, info, &argc, args , nullptr, nullptr);

    napi_valuetype valuetype0;
    napi_typeof(env, args[0], &valuetype0);

    napi_valuetype valuetype1;
    napi_typeof(env, args[1], &valuetype1);

    double value0;
    napi_get_value_double(env, args[0], &value0);

    double value1;
    napi_get_value_double(env, args[1], &value1);

    napi_value sum;
    napi_create_double(env, value0 + value1, &sum);

    return sum;
}

static napi_value dealPic(napi_env env, napi_callback_info info)
{
    OH_LOG_INFO(LOG_APP, "haha dealPic");
    size_t requireArgc = 3;
    size_t argc = 3;
    napi_value args[3] = {nullptr};
    napi_value sum = 0;
    
    napi_get_cb_info(env, info, &argc, args , nullptr, nullptr);

    napi_value input_array = args[0];
    bool is_typedarray;
    napi_is_typedarray(env, input_array, &is_typedarray);
    if(is_typedarray){
        OH_LOG_INFO(LOG_APP, "haha dealPic input_array is typedarray");
    }else{
        OH_LOG_INFO(LOG_APP, "haha dealPic input_array is not typedarray");
    }
    
    napi_typedarray_type type;
    napi_value input_buffer;
    size_t byte_offset;
    size_t i, length;
    napi_status status = napi_get_typedarray_info(env, input_array, &type, &length, NULL, &input_buffer, &byte_offset);
    if(status== napi_ok){
        OH_LOG_INFO(LOG_APP, "haha dealPic napi_get_typedarray_info ok");
    }else{
        OH_LOG_INFO(LOG_APP, "haha dealPic napi_get_typedarray_info failed");
    }
    
    void* data;
    size_t byte_length;
    status = napi_get_arraybuffer_info(env, input_buffer, &data, &byte_length);
    if(status== napi_ok){
        OH_LOG_INFO(LOG_APP, "haha dealPic napi_get_arraybuffer_info ok");
    }else{
        OH_LOG_INFO(LOG_APP, "haha dealPic napi_get_arraybuffer_info failed");
    }
    
    
    
    napi_value output_buffer;
    void* output_ptr = NULL;
    status = napi_create_arraybuffer(env, byte_length, &output_ptr, &output_buffer);
    napi_value output_array;
    status = napi_create_typedarray(env, type, length, output_buffer, byte_offset, &output_array);

    if (type == napi_uint8_array) {
        OH_LOG_INFO(LOG_APP, "haha dealPic type is napi_uint8_array byte_offset=%{public}d length =%{public}d ",byte_offset, length);
        uint8_t* input_bytes = (uint8_t*)(data) + byte_offset;
        uint8_t* output_bytes = (uint8_t*)(output_ptr);
        for (i = 0; i < length; i++) {
            uint8_t multiplier = 1;
            output_bytes[i] = (uint8_t)(input_bytes[i] * multiplier);
        }
    }else {
        napi_throw_error(env, NULL, "Typed array was of a type not expected by test.");
        return NULL;
    }
    
    return output_array;
}

EXTERN_C_START
static napi_value Init(napi_env env, napi_value exports)
{
    napi_property_descriptor desc[] = {
        { "add", nullptr, Add, nullptr, nullptr, nullptr, napi_default, nullptr },
        { "dealPic", nullptr, dealPic, nullptr, nullptr, nullptr, napi_default, nullptr }
    };
    napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
    return exports;
}
EXTERN_C_END

static napi_module demoModule = {
    .nm_version =1,
    .nm_flags = 0,
    .nm_filename = nullptr,
    .nm_register_func = Init,
    .nm_modname = "entry",
    .nm_priv = ((void*)0),
    .reserved = { 0 },
};

extern "C" __attribute__((constructor)) void RegisterEntryModule(void)
{
    napi_module_register(&demoModule);
}
